#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"



/********************************************************************************************
********************************************************************************************
    NT37710  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    NT37710  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    NT37710  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    NT37710  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    NT37710  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void NT37710_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		if(SigMode != CMD_Mode)
			SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		else
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void NT37710_Write_51Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		else
				SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    delay_ms(15);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void NT37710_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03; 
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]-1); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);

    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
	buffer[0]=0x2A;	
	buffer[1]=0x02;
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}

void NT37710_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,k=0,temp;
	
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		else
				SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);

    buffer1[0] = 0x06;       
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x00;                         // Page 2
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
	  delay_ms(5);  

    buffer1[0] = 0x03;       
    buffer1[1] = 0xd9;
	  buffer1[2] = 0x00;
    buffer1[3] = 0x10;	
	  SSD2828_W_Array(SigMode,channel,buffer1,0);		// 
	 // delay_ms(5);  	
	
    buffer1[0] = 0x06;       
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x02;                         // Page 2
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
	  delay_ms(5);  

    buffer1[0] = 0x02;       
    buffer1[1] = 0xbf;
    buffer1[2] = (buffer[3]>>4)&0x0f;  	
	  SSD2828_W_Array(SigMode,channel,buffer1,0);		// 

			for(j=0;j<=2;j++)
			{
				if((buffer[3]&0x0f)==0x00) //re
				{
					switch(j)
					{
							case 0: temp = 0xB0;break; //RED         
							case 1: temp = 0xB1;break;
							case 2: temp = 0xB2;break;
							default :break;			
					}
				}
				else if((buffer[3]&0x0f)==0x01)
				{
					switch(j)
					{
							case 0: temp = 0xB3;break; //GREEN      
							case 1: temp = 0xB4;break;
							case 2: temp = 0xB5;break;
							default :break;			
					}
				}
				else if((buffer[3]&0x0f)==0x02)
				{
					switch(j)
					{
							case 0: temp = 0xB6;break; //BLUE         
							case 1: temp = 0xB7;break;
							case 2: temp = 0xB8;break;
							default :break;			
					}
				}	
				SSD2828_W_Reg(SigMode,channel,0xBC,17); 
				SSD2828_W_Cmd(SigMode,channel,0xBF);	
				SSD2828_W_Data(SigMode,channel,temp); //red-> b0,b1,b2
				for( i = 0;i<16;i++)   //23个绑点
				{	                                                                                  
					SSD2828_W_Data(SigMode,channel,buffer[k+4]);	
					k++;  
				}
			}	
			buffer[4] = Uart_Error_None;
			buffer[1] =  0x08;
			STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 		

			buffer1[0] = 0x01;       
			buffer1[1] = 0x11;
			SSD2828_W_Array(SigMode,channel,buffer1,0);		// 
			delay_ms(2);
			buffer1[0] = 0x06;       
			buffer1[1] = 0xF0;
			buffer1[2] = 0x55;
			buffer1[3] = 0xAA;
			buffer1[4] = 0x52;
			buffer1[5] = 0x08;
			buffer1[6] = 0x00;                         // Page 2
			SSD2828_W_Array(SigMode,channel,buffer1,0);		

			buffer1[0] = 0x03;       
			buffer1[1] = 0xd9;
			buffer1[2] = 0x00;
			buffer1[3] = 0x00;	
			SSD2828_W_Array(SigMode,channel,buffer1,0);		// 
			delay_ms(5); 
		  
			SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void NT37710_Write_Gamma_Short_Package(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,k=0,x;
		u32 temp_cx=0;
		//SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	  SSD2828_W_Reg(SigMode,channel,0xB7,0x005b);
		delay_ms(10);

    buffer1[0] = 0x06;       
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x02;                         // Page 2
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
	  delay_ms(5);  

		for(x=0;x<3;x++)
		{
				switch(x)
				{
					case 0: buffer[3] = 0x00;break; //RED//GREEN//BLUE         
					case 1: buffer[3] = 0x42;break;
					case 2: buffer[3] = 0x81;break;
					default :break;			
				}	
				for(j=0;j<3;j++)
				{
					if(buffer[3]==0x00)
					{
						switch(j)
						{
								case 0: buffer1[1] = 0xc3;break; //RED//GREEN//BLUE         
								case 1: buffer1[1] = 0xc4;break;
								case 2: buffer1[1] = 0xc5;break;
								default :break;			
						}
					}
					else if(buffer[3]==0x42)
					{
						switch(j)
						{
								case 0: buffer1[1] = 0xc6;break; //RED//GREEN//BLUE         
								case 1: buffer1[1] = 0xc7;break;
								case 2: buffer1[1] = 0xc8;break;
								default :break;			
						}
					}
					else if(buffer[3]==0x81)
					{
						switch(j)
						{
								case 0: buffer1[1] = 0xc9;break; //RED//GREEN//BLUE         
								case 1: buffer1[1] = 0xca;break;
								case 2: buffer1[1] = 0xcb;break;
								default :break;			
						}
					}			
					SSD2828_W_Reg(SigMode,channel,0xBC,17); 
					SSD2828_W_Cmd(SigMode,channel,0xBF);	
					SSD2828_W_Data(SigMode,channel,buffer1[1]); //red-> b0,b1,b2
					for( i = 0;i<8;i++)   //23个绑点
					{	                 
						temp_cx=((buffer[k+4]<<8)+buffer[k+5])*4;
						
						SSD2828_W_Data(SigMode,channel,temp_cx>>8);
						SSD2828_W_Data(SigMode,channel,temp_cx&0x00ff);
						delay_ms(2);//延时实测必要
						k+=2;  
					}
				//	
				}	
			//	delay_ms(5);
			}
			buffer[4] = Uart_Error_None;
			buffer[3] =	 0x0C;
			buffer[1] =  0x08;
			STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 		

			SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void NT37710_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u16 tmp;
    u8 i,j,k=0;
    //SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
    buffer1[0] = 0x06;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x00;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0); 		
    buffer1[0] = 0x03;
    buffer1[1] = 0xd6;
    buffer1[2] = 0x00;
    buffer1[3] = 0x10;                      // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0); 		

    buffer1[0] = 0x06;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x02;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0); 	
    delay_ms(5);   
	
    buffer1[0] = 0x02;       
    buffer1[1] = 0xBF;
    buffer1[2] = (buffer[3]>>4)&0x0f;                      // Page 2
    SSD2828_W_Array(SigMode,channel,buffer1,0);		// Page 2
	  delay_ms(5); 
  
    //SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);
		SSD2828_W_Reg(SigMode,channel,0xB7,(LP_B7_Data|0x0080));
    delay_ms(5);
		for(j=0;j<3;j++)	
		{				
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0010); //返回个数设置
			buffer1[0] = 0x01;
			if((buffer[3]&0x0f)==0x00)
			{
				switch(j)
				{
					case 0: buffer1[1] = 0xB0;break; //RED//GREEN//BLUE         
					case 1: buffer1[1] = 0xB1;break;
					case 2: buffer1[1] = 0xB2;break;
					default :break;
				}
			}
			else if((buffer[3]&0x0f)==0x01)
			{
				switch(j)
				{
					case 0: buffer1[1] = 0xB3;break; //RED//GREEN//BLUE         
					case 1: buffer1[1] = 0xB4;break;
					case 2: buffer1[1] = 0xB5;break;
					default :break;
				}
			}	
			else if((buffer[3]&0x0f)==0x02)
			{
				switch(j)
				{
					case 0: buffer1[1] = 0xB6;break; //RED//GREEN//BLUE         
					case 1: buffer1[1] = 0xB7;break;
					case 2: buffer1[1] = 0xB8;break;
					default :break;
				}
			}					
			SSD2828_W_Array(SigMode,channel,buffer1,0);              
			delay_ms(5);  
			SSD2828_W_Cmd(SigMode,channel,0xFF);
			for(i=0;i<8;i++)
			{
					SSD2828_W_Cmd(SigMode,channel,0xFA);		
					tmp=SPI3_Read_u16_Data(channel);
					buffer[4+k]=tmp>>8;
					buffer[5+k]=tmp;
					delay_ms(3);
					k+=2;
			}
		}	
    buffer1[0] = 0x06;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x00;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0); 		
    buffer1[0] = 0x03;
    buffer1[1] = 0xd6;
    buffer1[2] = 0x00;
    buffer1[3] = 0x00;                      // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0); 		
		
    buffer[0]=0x2A;	//NT37710
    buffer[1]=0x09;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  		

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);

}

void NT37710_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[10];
//	  u8 mtp_flag;
	  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(50); 
    buffer1[0] = 0x01;
    buffer1[1] = 0x10;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(50); 
    buffer1[0] = 0x01;
    buffer1[1] = 0x28;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(15); 

    buffer1[0] = 0x01;
    buffer1[1] = 0x11;
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(150); 	
	
    buffer1[0] = 0x06;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x01;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0);  
//Page1 EBh, MTP_POSEL_REG=1
//    buffer1[0] = 0x04;
//    buffer1[1] = 0xEA;
//    buffer1[2] = 0x87;
//    buffer1[3] = 0x78;
//    buffer1[4] = 0x01;    
//		SSD2828_W_Array(SigMode,channel,buffer1,0); 
//    delay_ms(30);  
//internal power
    buffer1[0] = 0xB7;
    buffer1[1] = 0x18;    
		SSD2828_W_Array(SigMode,channel,buffer1,0); 		

    buffer1[0] = 0x04;
    buffer1[1] = 0xEB;
    buffer1[2] = 0x87;
    buffer1[3] = 0x78;
    buffer1[4] = 0x01;    
		SSD2828_W_Array(SigMode,channel,buffer1,0); 		
		delay_ms(30);  		
    buffer1[0] = 0x02;
    buffer1[1] = 0xCA;
    buffer1[2] = 0x01;   
		SSD2828_W_Array(SigMode,channel,buffer1,0); 		
		delay_ms(10); 
    buffer1[0] = 0x03;
    buffer1[1] = 0xCA;
    buffer1[2] = 0x01;   
		buffer1[3] = 0x02;  
		SSD2828_W_Array(SigMode,channel,buffer1,0); 			
		delay_ms(2); 
	//--------
		buffer1[0] = 0x09;
		buffer1[1] = 0xED;
		buffer1[2] = buffer[4];
		buffer1[3] = buffer[5];
		buffer1[4] = buffer[6];
		buffer1[5] = buffer[7];
		buffer1[6] = buffer[8];                
		buffer1[7] = buffer[9];
		buffer1[8] = buffer[10];
		buffer1[9] = 0x00;  
		SSD2828_W_Array(SigMode,channel,buffer1,0);  
	//---------
		delay_ms(80); 
    buffer1[0] = 0x04;
		buffer1[1] = 0xEE;
    buffer1[2] = 0xA5;
    buffer1[3] = 0x5A;   
		buffer1[4] = 0x3C;  
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(3000); 

    buffer1[0] = 0x02;
    buffer1[1] = 0xEC;
		buffer1[1] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
	//--------
		buffer1[0] = 0x09;
		buffer1[1] = 0xED;
		buffer1[2] = 0x00;
		buffer1[3] = 0x00;
		buffer1[4] = 0x00;
		buffer1[5] = 0x00;
		buffer1[6] = 0x00;                
		buffer1[7] = 0x00;
		buffer1[8] = 0x00;
		buffer1[9] = 0x00;  
		SSD2828_W_Array(SigMode,channel,buffer1,0);  
	//---------		
		buffer[2] = 0x02;
		buffer[3] = Uart_Error_None;   //返回 OTP Gamma数据  ：OK
		buffer[4] = 0;
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);         
}

void ProcessForIc2A( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
				case 0x01:                                      //写寄存器
					NT37710_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
				case 0x02:                                      //读寄存器
					NT37710_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
				case 0x03:                                      //写寄存器
					NT37710_Write_51Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;															
				case 0x08:                                      //写Gamma数据    
					NT37710_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
				case 0x09:                                      //读Gamma数据
					NT37710_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break; 
				case 0x0A://gamma otp
					NT37710_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;
				case 0x0C:
						NT37710_Write_Gamma_Short_Package(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
				default:	break;	
	}			
}


